package com.park.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.park.entity.User;
import com.park.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService us;

    //修改密码页面
    @GetMapping("pwd_update.html")
    public String login1() {
        return "pwd_update";
    }

    //用户资料编辑页面
    @GetMapping("info_edit.html")
    public String login2() {
        return "info_edit";
    }

    //用户管理页面
    @GetMapping("member.html")
    public String login3() {
        return "member";
    }

    //分页查所有
    @GetMapping("/parking/userlist")
    @ResponseBody
    public  List<User> userList(@RequestParam(value="current",required=false)int current){
        IPage<User> page= us.selectPage(current);
       return page.getRecords();
    }

    //分页总数
    @GetMapping("/parking/totalpage")
    @ResponseBody
    public  long totalPage(){
        IPage<User> page= us.selectPage(1);
        long total=page.getTotal()/page.getSize();
        if(page.getTotal()%page.getSize()!=0){
            total +=1;
        }
        return total;
    }

    //模糊查询
    @GetMapping("/parking/searchuser")
    @ResponseBody
    public List<User> searchByCarId(@RequestParam(value = "carId", required = false) String carId,@RequestParam(value="current",required=false)int current) {
        return us.getByUserCarId(carId,current);
    }

    //模糊查询总页数
    @GetMapping("/parking/totalpage2")
    @ResponseBody
    public  long totalPage2(@RequestParam(value = "carId", required = false) String carId){
        int page= us.getPage(carId);
        long total=page/6;
        if(page%6!=0){
            total +=1;
        }
        return total;
    }

    //根据手机号查询
    @GetMapping("/parking/selectbyphone")
    @ResponseBody
    public User selectbyphone(@RequestParam(value = "userPhone", required = false) String userPhone){
        return us.selectByPhone(userPhone);
    }

    //删除用户
    @GetMapping("/parking/userDel")
    @ResponseBody
    public String userDel(HttpSession session, @RequestParam("userId")int userId) {
        int result=us.delUser(userId);
        Map<String,String> map=new HashMap<String,String>();
        if(result==1) {
            map.put("delResult","true");
        }else {
            map.put("delResult","false");
        }
        return JSON.toJSONString(map);

    }

    //新增用户
    @GetMapping("/parking/userAdd")
    @ResponseBody
    public String addUser(@RequestParam("userName")String userName,@RequestParam("userCarId")String userCarId,@RequestParam("userPhone")String userPhone,@RequestParam("userBalance")double userBalance){
        User user=new User();
        user.setUserName(userName);
        user.setUserCarId(userCarId);
        user.setUserPhone(userPhone);
        user.setUserBalance(userBalance);
        int result=us.addUser(user);
        Map<String,String> map=new HashMap<String,String>();
        if(result==1) {
            map.put("addResult","true");
        }else {
            map.put("addResult","false");
        }
        return JSON.toJSONString(map);
    }

    //修改用户
    @GetMapping("/parking/userUpd")
    @ResponseBody
    public String updUser(@RequestParam(value = "userId",required = false)int userId,
                          @RequestParam(value = "userName",required = false)String userName,
                          @RequestParam(value = "userPwd",required = false)String userPwd,
                          @RequestParam(value = "userPhone",required = false)String userPhone,
                          @RequestParam(value = "userCarId",required = false) String userCarId,
                          @RequestParam(value = "userBalance",required = false)double userBalance,
                          @RequestParam(value = "userAmount",required = false)double userAmount,
                          @RequestParam(value = "userStatus",required = false)int userStatus){
        User user=new User();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setUserPwd(userPwd);
        user.setUserPhone(userPhone);
        user.setUserCarId(userCarId);
        user.setUserBalance(userBalance);
        user.setUserStatus(userStatus);
        user.setUserAmount(userAmount);
        int result=us.updUser(user);
        Map<String,String> map=new HashMap<String,String>();
        if(result==1) {
            map.put("updResult","true");
        }else {
            map.put("updResult","false");
        }
        return JSON.toJSONString(map);

    }
}



